﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using XmlToJson;
using System.Xml;
using System.Xml.Linq;

namespace kvv_forms
{
    public partial class ConvertorsView : Form
    {
        ConvertorController CController = new ConvertorController();
        public ConvertorsView()
        {
            InitializeComponent();
        }

        private void ConvertButton_Click(object sender, EventArgs e)
        {
            bool readAttrib = true, readComment = true, readCdata = true, readTextNode = true,  markingUsing=true;
            if (PrivateConfig.Checked)
            {
                readAttrib = AttribCheck.Checked;
                readComment = CommentCheck.Checked;
                readCdata = CdataCheck.Checked;
                readTextNode = TextNodeCheck.Checked;
                if (UseMarking.Checked == false)
                    JsonTextBox.WordWrap = true;
                markingUsing = UseMarking.Checked;
            }
            try
            {
                XDocument Xmldoc = XDocument.Parse(XmlTextBox.Text);
                JsonTextBox.Text = CController.Query(XmlTextBox.Text, readAttrib, readComment, readCdata, readTextNode, markingUsing);
            }
            catch
            {
                MessageBox.Show("ХML-данные не прошли проверку на валидность");
            }
        }

        private void DefaultConfig_Click(object sender, EventArgs e)
        {
            DefaultConfig.Checked = true;
            PrivateConfig.Checked = false;
        }

        private void PrivateConfig_Click(object sender, EventArgs e)
        {
            PrivateConfig.Checked = true;
            DefaultConfig.Checked = false;
        }

        private void SaveJson_Click(object sender, EventArgs e)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.Filter = "Json-файлы (*.json)|*.json|Все файлы (*.*)|*.*";
            if (s.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(s.FileName, JsonTextBox.Text, Encoding.Default);
            }
            else
                MessageBox.Show("Не удалось сохранить файл");
        }

        private void LoadXml_Click(object sender, EventArgs e)
        {
            XmlTextBox.Text = "";
            OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "Файл";
            fd.Filter = "XML-файлы (*.xml)|*.xml|Все файлы (*.*)|*.*";

            if (fd.ShowDialog() == DialogResult.OK)
            {
                XmlTextBox.Text = File.ReadAllText(fd.FileName, Encoding.Default);
            }
            else
                MessageBox.Show("Не удалось открыть файл");
        }

        private void ClearForm_Click(object sender, EventArgs e)
        {
            XmlTextBox.Text = "";
            JsonTextBox.Text = "";
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void GenerateXML_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            XmlTextBox.Text = File.ReadAllText("Example.xml", Encoding.Default);
        }

        private void UseTransfer_Click(object sender, EventArgs e)
        {
            
            if (UseTransfer.Checked)
            {
                UseTransfer.Checked = false;
                 XmlTextBox.WordWrap = false;
                JsonTextBox.WordWrap = false;
                
            }
            else
            {
                UseTransfer.Checked = true;
                XmlTextBox.WordWrap = true;
                JsonTextBox.WordWrap = true;
            }
         }

       
    }
}
